<?php
namespace App\Http\Controllers;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
class RoleController extends CommonController
{

    public function index()
    {
        dump('role');
    }

    // 示例方法，用于展示后台首页
    public function getAll(Request $request)
    {
        $query = Role::query();
        //分页查询
        $items = $query->select('id','name')->get();
        if($items){
            return re_success('操作成功',$items);
        }else{
            return re_warning('暂无数据',null);

        }
    }
    // 示例方法，用于展示后台首页
    public function getlist(Request $request)
    {
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $status = $request->input('status',null);
        $name = $request->input('name',null);

        $query = Role::query();
        $query->orderBy('id', 'desc');
        //状态条件
        if($status){
            $query->where('status','=',$status);
        }
        //多个查询
        if($name){
            $query->where('name','like', '%'.$name.'%');
            $query->orWhere('content','like', '%'.$name.'%');
        }

        //分页查询
        $items = $query->paginate( $limit,['*'],'page',$page);
//        $users->getCollection()->makeHidden(['password', 'key']);
        //返回对应格式
        $data = [
            "data"=>$items->items() ,
            "total"=>$items->total(),
            "limit"=>$items->perPage(),
            "page"=>$items->currentPage(),
            "last_page"=>$items->lastPage()
        ];
        if($items->total() == 0){
            return re_warning('暂无数据',null);
        }else{
            return re_success('操作成功',$data);
        }
    }
    // 添加角色
    public function create(Request $request)
    {
        // 逻辑代码
        $data = $request->post();
        $data['created_at'] = date('Y-m-d H:i:s');
        $data['status'] = 1;
//        if($data['role']){
//            $data['role'] = json_encode($data['role']);
//        }
        //创建数据
        $inserted = Role::create($data);
        if ($inserted) {
            // 插入成功
           return  re_success("添加成功",$inserted);
        } else {
            // 插入失败
            return  re_danger("添加失败",null);

        }
    }
    /**
     * 修改状态
     * @param Request $request
     * @return void
     */
    public function status(Request $request)
    {
        $data = $request->all();
        $model = Role::find($data['id']);
        $model->status = $model->status === 1 ? 2 : 1 ;
        $info =   $model->save();
        if ($info) {
            return  re_success("修改成功",null);
        } else {
            return  re_danger("修改失败",null);
        }

    }
    public function update(Request $request)
    {
        $data = $request->all();
//        $data['roles'] = json_encode($data['roles']);
        $data['updated_at'] = date('Y-m-d H:i:s');
        $model = new Role();
        $info =   $model->where('id',$data['id'])->update($data);
        if ($info) {
            return  re_success("修改成功",null);
        } else {
            return  re_danger("修改失败",null);
        }
    }

    public function delete(Request $request){
        $id = $request->input('id');
        $info= Role::destroy($id);
        if ($info) {
            return  re_success("删除成功",null);
        } else {
            return  re_danger("删除失败",null);
        }
    }

    public function info(Request $request)
    {
        $info = Role::find($request->id);
        if($info){
            return  re_success("添加成功",$info);
        }else{
            return  re_warning("没有该数据",null);

        }
    }

}
